home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl2 / examples / nurbs / mask.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  8.0 KB  |  294 lines

  1. /*
  2.  * Copyright 1993, 1995, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /* mask.c
  19.  * This program draws a rotating nurbs surface shaped like a mask.
  20.  *
  21.  *    Escape key        - exit the program
  22.  *    <t> key        - toggle texture mapping on/off
  23.  */
  24. #include <GL/gl.h>
  25. #include <GL/glu.h>
  26. #include <GL/glut.h>
  27.  
  28. #include <math.h>
  29. #include <stdio.h>
  30.  
  31. #include "rgbImageFile.h"        /* should be in ../../include */
  32.  
  33. /*  Function Prototypes  */
  34.  
  35. GLvoid  initgfx( GLvoid );
  36. GLvoid  drawScene( GLvoid );
  37. GLvoid  reshape( GLsizei, GLsizei );
  38. GLvoid  animate( GLvoid );
  39. GLvoid  visibility( GLint );
  40. GLvoid  keyboard( GLubyte, GLint, GLint );
  41.  
  42. GLvoid initSurface( GLvoid );
  43. GLvoid initTexture( unsigned int *image, 
  44.     GLsizei imageWidth, GLsizei imageHeight );
  45. GLvoid toggleTexture( GLvoid );
  46.  
  47. void printHelp( char * );
  48.  
  49. /* Global Definitions */
  50.  
  51. #define KEY_ESC    27    /* ascii value for the escape key */
  52.  
  53. /* Global Variables */
  54.  
  55. static GLboolean textureFlag = GL_FALSE;
  56.     
  57. static GLfloat ctlpoints[10][7][3] = 
  58. {  /*   t = 0,         t = 1,        t = 2,        t = 3,        t = 4,        t = 5,        t = 6   */
  59. { {-1.4,7.1,0.0},{-1.2,7.1,0.2},{-1.0,7.1,0.3},{0.0,7.1,0.5},{1.0,7.1,0.3},{1.2,7.1,0.2},{1.4,7.1,0.0} }, /* s=0 */
  60. { {-1.6,7.0,0.0},{-1.4,7.0,0.7},{-1.0,7.0,0.9},{0.0,7.0,1.1},{1.0,7.0,0.9},{1.4,7.0,0.7},{1.6,7.0,0.0} }, /* s=1 */
  61. { {-1.8,4.5,0.0},{-1.5,4.5,0.5},{-1.0,4.5,0.7},{0.0,4.5,0.9},{1.0,4.5,0.7},{1.5,4.5,0.5},{1.8,4.5,0.0} }, /* s=2 */
  62. { {-1.9,2.6,0.0},{-1.5,2.6,1.1},{-1.0,2.6,1.0},{0.0,2.6,2.5},{1.0,2.6,1.0},{1.5,2.6,1.1},{1.9,2.6,0.0} }, /* s=3 */
  63. { {-1.8,2.8,0.0},{-1.5,2.8,0.8},{-1.0,2.8,0.9},{0.0,2.8,1.0},{1.0,2.8,0.9},{1.5,2.8,0.8},{1.8,2.8,0.0} }, /* s=4 */
  64. { {-1.8,1.8,0.0},{-1.5,1.8,0.8},{-1.0,1.8,1.0},{0.0,1.8,1.2},{1.0,1.8,1.0},{1.5,1.8,0.8},{1.8,1.8,0.0} }, /* s=5 */
  65. { {-1.8,1.6,0.0},{-1.5,1.6,0.7},{-1.0,1.6,0.8},{0.0,1.6,0.9},{1.0,1.6,0.8},{1.5,1.6,0.7},{1.8,1.6,0.0} }, /* s=6 */
  66. { {-1.7,0.9,0.0},{-1.5,0.9,0.8},{-1.0,0.9,1.0},{0.0,0.9,1.1},{1.0,0.9,1.0},{1.5,0.9,0.8},{1.7,0.9,0.0} }, /* s=7 */
  67. { {-1.5,.12,0.0},{-1.2,.12,0.6},{-1.0,.12,0.7},{0.0,.12,0.8},{1.0,.12,0.7},{1.2,.12,0.6},{1.5,.12,0.0} }, /* s=8 */
  68. { {-0.8,.15,0.0},{-1.1,.15,0.0},{-1.0,.15,0.0},{0.0,.15,0.0},{1.0,.15,0.0},{1.1,.15,0.0},{0.8,.15,0.0} } /* s=9 */
  69. };
  70.  
  71. static GLUnurbsObj *theNurb;
  72.  
  73. static unsigned int     *image;
  74. static GLsizei        imageWidth, imageHeight;
  75.  
  76. static GLfloat angle = 0.0;
  77.  
  78. void
  79. main(int argc, char *argv[])
  80. {
  81.     GLsizei     width, height;
  82.     char        *imageFileName = "cv.rgb";
  83.  
  84.     fprintf(stdout, "loading image %s\n", imageFileName );
  85.     image = rgbReadImageFile(imageFileName, &imageWidth,
  86.              &imageHeight);
  87.  
  88.     glutInit( &argc, argv );
  89.  
  90.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  91.     height = glutGet( GLUT_SCREEN_HEIGHT );
  92.     glutInitWindowPosition( width/4, height/4 ); 
  93.     glutInitWindowSize( width/2, height/2 );
  94.     glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
  95.     glutCreateWindow( argv[0] );
  96.     
  97.     initgfx();
  98.  
  99.     glutKeyboardFunc( keyboard );
  100.     glutReshapeFunc( reshape );
  101.     glutIdleFunc( animate ); 
  102.     glutVisibilityFunc( visibility ); 
  103.     glutDisplayFunc( drawScene ); 
  104.  
  105.     printHelp( argv[0] );
  106.  
  107.     glutMainLoop();
  108. }
  109.  
  110. void
  111. printHelp( char *progname )
  112. {
  113.     fprintf(stdout, "\n%s - draw a rotating mask made of "
  114.         "a NURBS surface with trimming curves.\n\n"
  115.         "Escape key        - exit the program\n"
  116.         "<t> key        - toggle texture mapping on/off\n\n",
  117.         progname);
  118. }
  119.  
  120. GLvoid
  121. initgfx( GLvoid )
  122. {
  123.     GLfloat mat_diffuse[] = { 1.0, 0.6, 0.0, 1.0 };
  124.     GLfloat mat_emission[] = { 0.0, 0.0, 0.0, 1.0 };
  125.  
  126.     GLfloat light_position[] = { 1.0, 0.0, 1.0, 0.0 };
  127.  
  128.     glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat_diffuse);
  129.     glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, mat_emission);
  130.     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  131.  
  132.     glEnable(GL_LIGHTING);
  133.     glEnable(GL_LIGHT0);
  134.  
  135.     glClearColor( 0, 0, 0.8, 1 );
  136.     glEnable( GL_DEPTH_TEST );
  137.  
  138.     /* enable automatic surface normal generation */
  139.     glEnable( GL_AUTO_NORMAL );
  140.  
  141.     theNurb = gluNewNurbsRenderer();
  142.  
  143.     initTexture( image, imageWidth, imageHeight );
  144. }
  145.  
  146. GLvoid initTexture( unsigned int *image, 
  147.     GLsizei imageWidth, GLsizei imageHeight )
  148. {
  149.     /* scale texture image, make mipmaps and load texture
  150.      *    gluBuild2DMipmaps( target, components, width, height,
  151.      *        format,  type, imageArray ) 
  152.      */
  153.     gluBuild2DMipmaps(GL_TEXTURE_2D, 4, imageWidth, imageHeight,
  154.              GL_RGBA, GL_UNSIGNED_BYTE, image);
  155.  
  156.                         
  157.     /* Decal uses only the texture color, not the original polygon 
  158.      * color, unless there is alpha in texture, in which case
  159.      * alpha determines the percentage of texture blended
  160.      * with the polygon color.
  161.      *
  162.      * Decal may be faster than the default modulate mode.
  163.      */
  164.     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
  165.     
  166.     /* Set texture coordinate generation function to use 
  167.      * spherical coordinate mapping
  168.      */
  169.     glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
  170.     glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
  171. }
  172.  
  173. GLvoid  toggleTexture( GLvoid )
  174. {
  175.     textureFlag = !textureFlag;
  176.     if (textureFlag)
  177.     {
  178.         glEnable(GL_TEXTURE_GEN_S);
  179.         glEnable(GL_TEXTURE_GEN_T);
  180.         glEnable(GL_TEXTURE_2D);
  181.     }
  182.     else
  183.     {
  184.         glDisable(GL_TEXTURE_GEN_S);
  185.         glDisable(GL_TEXTURE_GEN_T);
  186.         glDisable(GL_TEXTURE_2D);
  187.     }    
  188. }
  189.  
  190. GLvoid 
  191. keyboard( GLubyte key, GLint x, GLint y )
  192. {
  193.     switch (key) {
  194.     case 't':    /* toggle texture mapping on/off */
  195.         toggleTexture();
  196.         glutPostRedisplay();
  197.         break;
  198.     case KEY_ESC:    /* Exit whenever the Escape key is pressed */
  199.         exit(0);
  200.     }
  201. }
  202.  
  203. GLvoid
  204. animate( GLvoid )
  205. {
  206.     /* update the current angle */
  207.     angle = fmodf( (angle + 5.0), 360.0 );
  208.  
  209.     /* Tell GLUT to redraw the scene */
  210.     glutPostRedisplay();
  211. }
  212.         
  213. GLvoid
  214. visibility( int state )
  215. {
  216.     if (state == GLUT_VISIBLE) {
  217.         glutIdleFunc( animate );
  218.     } else {
  219.         glutIdleFunc( NULL );
  220.     }
  221. }
  222.  
  223. GLvoid
  224. reshape( GLsizei width, GLsizei height )
  225. {
  226.     GLdouble    aspect;
  227.  
  228.     glViewport( 0, 0, width, height );
  229.  
  230.     aspect = (GLdouble) width / (GLdouble) height;
  231.  
  232.     glMatrixMode( GL_PROJECTION );
  233.     glLoadIdentity();
  234.     gluPerspective( 45.0, aspect, 3.0, 25.0 );
  235.     glMatrixMode( GL_MODELVIEW );
  236.     glLoadIdentity();
  237.     glTranslatef( 0.0, 0.0, -20.0 ); 
  238. }
  239.  
  240. GLvoid
  241. drawScene( GLvoid )
  242. {
  243.     GLfloat knots[8] = {0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0};
  244.     GLfloat edgePt[5][2] = /* counter clockwise */
  245.         {{0.0, 0.0}, {7.0, 0.0}, {7.0, 8.0}, {0.0, 8.0}, {0.0, 0.0}};
  246.  
  247.     GLfloat curveKnots[] = 
  248.         {0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 2.0, 2.0, 2.0};
  249.     GLfloat leftEyeCurve[5][2] = /* clockwise */ 
  250.         {{1.3, 3.0}, {1.3, 1.0}, {1.1, 1.0}, {1.1, 3.0}, {1.3, 3.0}};
  251.     GLfloat rightEyeCurve[5][2] = /* clockwise */ 
  252.         {{1.3, 5.0}, {1.1, 5.0}, {1.1, 7.0}, {1.3, 7.0}, {1.3, 5.0}};
  253.     GLfloat s_knots[14] = 
  254.     { 
  255.         0.0,0.0,0.0,0.0,
  256.         1.0,2.0,3.0,4.0,5.0,6.0,
  257.         7.0,7.0,7.0,7.0
  258.     };
  259.  
  260.     GLfloat t_knots[11] = { 0.0,0.0,0.0,0.0,2.0,4.0,6.0,8.0,8.0,8.0,8.0 };
  261.  
  262.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  263.     glPushMatrix(); 
  264.         glTranslatef( 0.0, -4.0, 0.0 );    
  265.         glRotatef( angle, 0.0, 1.0, 0.0 );
  266.         gluBeginSurface(theNurb);
  267.             gluNurbsSurface(theNurb, 
  268.                 14, s_knots,
  269.                 11, t_knots,
  270.                 7 * 3, 3,
  271.                 &ctlpoints[0][0][0], 
  272.                 4, 4,
  273.                 GL_MAP2_VERTEX_3);
  274.             gluBeginTrim (theNurb);
  275.                 gluPwlCurve (theNurb, 5, &edgePt[0][0], 2,
  276.                     GLU_MAP1_TRIM_2);
  277.             gluEndTrim (theNurb);
  278.  
  279.             gluBeginTrim (theNurb);
  280.                 gluNurbsCurve (theNurb, 9, curveKnots, 2, 
  281.                     &leftEyeCurve[0][0], 4, GLU_MAP1_TRIM_2);
  282.             gluEndTrim (theNurb);
  283.             gluBeginTrim (theNurb);
  284.                 gluNurbsCurve (theNurb, 9, curveKnots, 2, 
  285.                     &rightEyeCurve[0][0], 4, GLU_MAP1_TRIM_2);
  286.             gluEndTrim (theNurb);
  287.         gluEndSurface(theNurb);
  288.  
  289.     glPopMatrix();
  290.  
  291.     glutSwapBuffers();
  292. }
  293.  
  294.